/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.NhomMonHocM;
import model.NhomMonTuChonM;
import util.DAL;
import util.MyConnectUnit;

public class NhomMonTuChonD {
	private MyConnectUnit conn;
	
	/**
	 * @throws Exception 
	 * @function constructor NhomMonTuChonD
	 */
	public NhomMonTuChonD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}
	
	/**
	 * @function get all NhomMonTuChon
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<NhomMonTuChonM> ls = new ArrayList<NhomMonTuChonM>();
		ResultSet rs = this.conn.Selects(
						"id_nhom_mon_tu_chon, nmtc.ten, nmtc.id_nhom_mon_hoc, nmh.ten",
						"NHOM_MON_TU_CHON nmtc, NHOM_MON_HOC nmh",
						"nmtc.id_nhom_mon_hoc = nmh.id_nhom_mon_hoc AND nmtc.ten LIKE '%" + ten + "%'",
						"nmh.ten ASC",
						limit);
		NhomMonTuChonM l = null;
		while (rs.next()) {
			l = new NhomMonTuChonM();
			l.setId_nhom_mon_tu_chon(rs.getInt("id_nhom_mon_tu_chon"));
			l.setTen(rs.getString("nmtc.ten"));
			
			l.setId_nhom_mon_hoc(rs.getInt("nmtc.id_nhom_mon_hoc"));
			NhomMonHocM nmh = new NhomMonHocM();
			nmh.setTen(rs.getString("nmh.ten"));
			l.setTen_nhom_mon_hoc(nmh);

			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all NhomMonTuChon
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<NhomMonTuChonM> ls = new ArrayList<NhomMonTuChonM>();
		ResultSet rs = this.conn.Select("NHOM_MON_TU_CHON", Condition, Limit, OrderBy);
		NhomMonTuChonM l = null;
		while (rs.next()) {
			l = new NhomMonTuChonM();
			l.setId_nhom_mon_tu_chon(rs.getInt("id_nhom_mon_tu_chon"));
			l.setTen(rs.getString("ten"));
			l.setId_nhom_mon_hoc(rs.getInt("id_nhom_mon_hoc"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all NhomMonTuChon with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all NhomMonTuChon with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all NhomMonTuChon no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten NhomMonTuChonM
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> searchNMTC(String ten) throws Exception {
		ArrayList<NhomMonTuChonM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all NhomMonTuChon with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NhomMonTuChonM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<NhomMonTuChonM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * function insert NhomMonTuChon
	 * @param mh
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(NhomMonTuChonM nmtc) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", nmtc.getTen());
		map.put("id_nhom_mon_hoc", nmtc.getId_nhom_mon_hoc());
		return this.conn.Insert("NHOM_MON_TU_CHON", map);
	}

	/**
	 * @function update NhomMonTuChon
	 * @param nmtc
	 * @return
	 * @throws Exception
	 */
	public boolean Update(NhomMonTuChonM nmtc) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", nmtc.getTen());
		map.put("id_nhom_mon_hoc", nmtc.getId_nhom_mon_hoc());
		return this.conn.Update("NHOM_MON_TU_CHON", map, "id_nhom_mon_tu_chon = " + nmtc.getId_nhom_mon_tu_chon());
	}

	/**
	 * @function delete NhomMonTuChon
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("NHOM_MON_TU_CHON", "id_nhom_mon_tu_chon = " + id);
	}
}
